//
//  WKWelcomeController.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/27.
//  Copyright © 2016年 恋guang年. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WKWelcomeController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //发送网络数据之前先使用本地图片 如果本地没有 就使用占位图片
        if let urlString = WKUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            let url = NSURL(string: urlString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        WKUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil{
                print("请求用户信息出错")
                return
            }
            //可选绑定 获得用户头像的urlString
            if let urlString = WKUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
                let url = NSURL(string: urlString)
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
        prepareForUI()
        
    }
    ///  控件已经显示
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }
    private func startAnimation(){
        //改变iconVeiw的底部约束
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        // 动画
        // usingSpringWithDamping: 弹簧名明显程度, 0 - 1, 数值越小,弹簧效果越明显
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(2, animations: { () -> Void in
                    self.wcLbel.alpha = 1
                    }, completion: { (_) -> Void in
                        //动画结束 切换AppDelegate的根控制器
                        AppDelegate.outSwitchAppDelegateRootViewController(WKMainViewController())
                })
        }
        
    }
    /// 准备UI
    private func prepareForUI(){
        /// 添加子控件
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(wcLbel)
        
        /// 为子控件添加约束
        //bgImageView
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        //iconView
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        //wcLbel
        wcLbel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    //MARK:- 懒加载子控件
    ///  背景
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///  头像
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //把头像切圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView;
    }()
    ///  欢迎Label
    private lazy var wcLbel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.text = "欢迎归来"
        label.alpha = 0
        return label
    }()
    
}
